package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.StudentService;


import java.util.Scanner;

public class StudentController {

    StudentService studentService = new StudentService();

    private Scanner sc = new Scanner(System.in);

    public void start() {
        studentLoop:while(true){
            System.out.println("----------欢迎来到学生管理系统---------");
            System.out.println("1、增加学生 2、删除学生 3、修改学生 4、查找学生 5、退出");
            System.out.println("请输入您的选择：");
            String choice = sc.next();
            switch (choice){
                case "1":
                    addStudent();
                    break;
                case "2":
                    deleteStudentById();
                    break;
                case "3":
                    updateStudent();
                    break;
                case "4":
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("退出!");
                    break studentLoop;
                default:
                    System.out.println("您输入的信息有误，请检查重新输入！");
                    break;
            }
        }

    }

    public void updateStudent() {
        String updateId = inputStudentId();

       Student newStu = inputStudentInfo(updateId);

        StudentService.updateStudent(updateId,newStu);

        System.out.println("修改成功！");
    }

    private void deleteStudentById() {
        String delId = inputStudentId();

        StudentService.deleteStudentById(delId);
        System.out.println("删除成功！");
    }

    private void findAllStudent() {
        Student[] stus = StudentService.findAllStudent();
        if(stus == null){
            System.out.println("查无信息，请添加后重试！");
            return;
        }
        System.out.println("学号\t\t\t姓名\t\t年龄\t\t\t生日");
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            if(stu != null){
                System.out.println(stu.getId() + "\t" + stu.getName() + "\t\t" +
                        stu.getAge() + "\t\t" +stu.getBirthday());
            }
        }
    }

    private void addStudent() {

        String id;
        while(true){
            System.out.println("请输入学生学号：");
            id = sc.next();
            //isExists 判断 ID是否存在
            boolean flag = StudentService.isExists(id);
            if(flag){
                System.out.println("学号已经存在，请重新输入：");
            }else{
                break;
            }
        }

        Student stu = inputStudentInfo(id);

        boolean result = studentService.addStudent(stu);
        if(result){
            System.out.println("添加成功！");
        }else{
            System.out.println("添加失败!");
        }
    }

    //键盘录入学生id
    public  String inputStudentId(){
        String id;
        while (true){
            System.out.println("请输入学生id：");
            id = sc.next();
            boolean exists = StudentService.isExists(id);
            if(!exists){
                System.out.println("您输入的id不存在，请重新输入：");
            }else{
                break;
            }
        }

        return id;
    }

    //键盘录入学生信息
    public  Student inputStudentInfo(String id){
        System.out.println("请输入学生姓名：");
        String name = sc.next();
        System.out.println("请输入学生年龄：");
        String age = sc.next();
        System.out.println("请输入学生生日：");
        String birthday = sc.next();

        Student stu = new Student();
        stu.setId(id);
        stu.setName(name);
        stu.setAge(age);
        stu.setBirthday(birthday);

        return stu;
    }
}
